module ecs{
    export class Scene {
        /** 清除preRender中用于清除屏幕的颜色 */
        public ClearColor: engine.Color = engine.Color.CornflowerBlue;

        public readonly _afterPostProcessorRenderers: FastList<Renderer> = new FastList<Renderer>();
        public _renderers: FastList<Renderer> = new FastList<Renderer>();

        private _sceneRenderTarget: egret.DisplayObject;

        private _didSceneBegin: boolean;
        /** 此场景中的实体列表 */
        public readonly Entities: EntityList;
        /** 默认场景相机 */
        public Camera: Camera;

        constructor(){
            this.Entities = new EntityList(this);
            
            var cameraEntity = this.CreateEntity("camera");
            this.Camera = cameraEntity.AddComponent(new Camera());

            if (Core.entitySystemsEnabled){
                //TODO: 实体解析
            }

            this.Initialize();
        }

        /** 将实体添加到此场景，然后返回 */
        public CreateEntity(name: string): Entity{
            var entity = new Entity(name);
            return this.AddEntity(entity);
        }

        /** 将实体添加到场景的实体列表 */
        public AddEntity(entity: Entity): Entity{
            if (this.Entities.Contains(entity)){
                throw new Error("您试图两次将同一实体添加到场景：" + entity);
            }

            this.Entities.Add(entity);
            entity.Scene = this;

            for (var i = 0; i < entity.Transform.ChildCount; i++){
                this.AddEntity(entity.Transform.GetChild(i).Entity);
            }
            
            return entity;
        }

        public static CreateWithDefaultRenderer(clearColor?: engine.Color): Scene{
            var scene = new Scene();

            scene.ClearColor = clearColor;
            scene.AddRenderer(new DefaultRenderer());
            return scene;
        }

        public Begin(){
            if (this._renderers.Length == 0){
                this.AddRenderer(new DefaultRenderer());
            }

            this._didSceneBegin = true;
            this.OnStart();
        }

        public End(){
            this._didSceneBegin = false;

            //TODO: 卸载所有资源
        }

        /** 在Scene子类中重写此方法，然后在此处进行加载。 这是在场景设置好之后但从未调用过的情况下从构造函数调用的。 */
        public Initialize(){}

        /** 在Scene子类中重写此方法。 当Core将此场景设置为活动场景时，将调用此方法。 */
        public OnStart(){}

        public Update(){
            this.Entities.UpdateLists();

            this.Entities.UpdateLists();
        }

        /**
         * 将渲染器添加到场景
         * @param renderer 
         */
        public AddRenderer<T extends Renderer>(renderer: T){
            if (renderer.WantsToRenderAfterPostProcessors){
                this._afterPostProcessorRenderers.Add(renderer);
                this._afterPostProcessorRenderers.Sort();
            }
            else{
                this._renderers.Add(renderer);
                this._renderers.Sort();
            }

            renderer.OnAddedToScene(this);

            /** 如果我们已经开始让PostProcessor知道RenderTarget的大小 */
            if (this._didSceneBegin)
                renderer.OnSceneBackBufferSizeChanged(this._sceneRenderTarget.width, this._sceneRenderTarget.height);

            return renderer;
        }
    }
}